/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatuidemo.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.chatuidemo.DemoApplication;
import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.service.UserService;
import com.easemob.chatuidemo.utils.FlyAntGlobalVar;
import com.easemob.chatuidemo.utils.StringUtils;

/**
 * 注册页
 * 
 */
public class RegisterActivity extends BaseActivity {
	private EditText userNameEditText;
	private EditText passwordEditText;
	private EditText confirmPwdEditText;
    private ProgressDialog progressDialog;
    private String userType = "商家";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		userNameEditText = (EditText) findViewById(R.id.username);
		passwordEditText = (EditText) findViewById(R.id.password);
		confirmPwdEditText = (EditText) findViewById(R.id.confirm_password);
	}

	/**
	 * 注册
	 * 
	 * @param view
	 */
	public void register(View view) {
		String st1 = getResources().getString(R.string.User_name_cannot_be_empty);
		String st2 = getResources().getString(R.string.Password_cannot_be_empty);
		String st3 = getResources().getString(R.string.Confirm_password_cannot_be_empty);
		String st4 = getResources().getString(R.string.Two_input_password);
		String st5 = getResources().getString(R.string.Is_the_registered);
		final String st6 = getResources().getString(R.string.Registered_successfully);
		final String username = userNameEditText.getText().toString().trim();
		final String pwd = passwordEditText.getText().toString().trim();
		String confirm_pwd = confirmPwdEditText.getText().toString().trim();
		if (TextUtils.isEmpty(username)) {
			Toast.makeText(this, st1, Toast.LENGTH_SHORT).show();
			userNameEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(pwd)) {
			Toast.makeText(this, st2, Toast.LENGTH_SHORT).show();
			passwordEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(confirm_pwd)) {
			Toast.makeText(this, st3, Toast.LENGTH_SHORT).show();
			confirmPwdEditText.requestFocus();
			return;
		} else if (!pwd.equals(confirm_pwd)) {
			Toast.makeText(this, st4, Toast.LENGTH_SHORT).show();
			return;
		} else if (!StringUtils.isPhoneNo(username)){
		    Toast.makeText(this, "请使用正确的手机号注册", Toast.LENGTH_SHORT).show();
		    return;
		}

		if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)) {
			final String st7 = getResources().getString(R.string.network_anomalies);
			final String st8 = getResources().getString(R.string.User_already_exists);
			final String st9 = getResources().getString(R.string.registration_failed_without_permission);
			final String st10 = getResources().getString(R.string.Registration_failed);
			
			new NetWorkTask().execute(username, pwd,userType);
//			new Thread(new Runnable() {
//				public void run() {
//					try {
////					    new UserService().register(username, pwd);
//						// 调用sdk注册方法
//						EMChatManager.getInstance().createAccountOnServer(username, pwd);
//						runOnUiThread(new Runnable() {
//							public void run() {
//							    
//								if (!RegisterActivity.this.isFinishing())
//									pd.dismiss();
//								// 保存用户名
//								DemoApplication.getInstance().setUserName(username);
//								Toast.makeText(getApplicationContext(), st6, 0).show();
//								finish();
//							}
//						});
//					} catch (final Exception e) {
//						runOnUiThread(new Runnable() {
//							public void run() {
//								if (!RegisterActivity.this.isFinishing())
//									pd.dismiss();
//								int errorCode=((EaseMobException) e).getErrorCode();
//								if(errorCode==EMError.NONETWORK_ERROR){
//									Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
//								}else if(errorCode==EMError.USER_ALREADY_EXISTS){
//									Toast.makeText(getApplicationContext(), st8, Toast.LENGTH_SHORT).show();
//								}else if(errorCode==EMError.UNAUTHORIZED){
//									Toast.makeText(getApplicationContext(), st9, Toast.LENGTH_SHORT).show();
//								}else{
//									Toast.makeText(getApplicationContext(), st10 + e.getMessage(), Toast.LENGTH_SHORT).show();
//								}
//							}
//						});
//					}
//				}
//			}).start();

		}
	}
	

	public void back(View view) {
		finish();
	}

	void progressDialogInit(){
	    progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(android.R.drawable.ic_menu_info_details);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
        progressDialog.setMessage(getResources().getString(R.string.Is_the_registered));
    }
    
    class NetWorkTask extends AsyncTask<String, Integer, String>{
        @Override
        protected void onPreExecute() {
            progressDialogInit();
            progressDialog.show();
            super.onPreExecute();
        }
        
        @Override
        protected String doInBackground(String... params) {
            UserService uService = new UserService();
            String retCode = uService.register(params[0], params[1],params[2]);
            return retCode;
        }
        @Override
        protected void onPostExecute(String result) {
            if ( result == null) {
                progressDialog.dismiss();
                AlertDialog.Builder builder = new Builder(RegisterActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.setMessage("网络连接失败,请检查网络情况");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        }
                    });
                builder.create().show();
                return;
            }
            if ("0000".equals(result)) {// 注册成功
                DemoApplication.getInstance().setUserName(FlyAntGlobalVar.hxacc);
                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }else{
                AlertDialog.Builder builder = new Builder(RegisterActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.setMessage(result);
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        }
                    });
                builder.create().show();
            }
            progressDialog.dismiss();
            super.onPostExecute(result);
        }
        
    }
}
